<?php

require_once('rb.php');
R::setup('mysql:host=ftp.giemiebier.nl:3306;dbname=personify', 'school', 'admin123');
//R::debug(true);
if ($pagetree[2] != "" && isset($pagetree[2])) {
    $id = $pagetree[2];
} else {
    $id = "";
}

$conn = R::getAll('select userid2 from connections where userid1 = :id', array(':id' => $id));
$return = "";

$i = 0;

foreach ($conn as $c) {
    
    $users = R::getAll('select * from profile where userid = :id',
            array(':id' => $c['userid2'])
    );
    $name = ",.";
    foreach ($users as $user) {
        $name = $user["name"]." ".$user["lastname"];
    }
    
    $return[$i]['user'] = $c['userid2'];
    $return[$i]['type'] = "connection";
    $return[$i]['name'] = $name;
    $i++;
}

$pending = R::getAll('select userid1,id from pending where userid2 = :id', array(':id' => $id));

foreach ($pending as $p) {

    $users = R::getAll('select * from profile where userid = :id',
            array(':id' => $p['userid1'])
    );
    $name = "Unknown";
    foreach ($users as $user) {
        $name = $user["name"]." ".$user["lastname"];
    }
    $return[$i]['id'] = $p['id'];
    $return[$i]['user'] = $p['userid1'];
    $return[$i]['type'] = "pending";
    $return[$i]['name'] = $name;
    $i++;
}

echo json_encode($return);
?>
